﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;

namespace GrooveSharp
{

    /// <summary>
    /// Defines consts, classes and functions used in Importer and in Exporter
    /// </summary>
    /// <remarks>
    /// We don't use registered mappings but define them inline using a lambda
    /// </remarks>
    static class ImpExp
    {
        // Define recurring terms in the XML in import/export
        public const string XML_ELEMENT_MAIN = "GrooveSharpData";
        public const string XML_ELEMENT_PLAY = "Play";
        public const string XML_ELEMENT_RATING = "Rating";
        public const string XML_ELEMENT_FLAG = "Flag";

        public const string XML_ATTRIBUTE_FILENAME = "filename";
        public const string XML_ATTRIBUTE_FOLDERNAME = "foldername";
        public const string XML_ATTRIBUTE_FLAG = "flagname";
        public const string XML_ATTRIBUTE_RATING = "rating";
        public const string XML_ATTRIBUTE_DATE = "date";
        public const string XML_ATTRIBUTE_DURATION = "duration";

        public class BaseInfo
        {
            public string FileName { get; set; }
            public string FolderName { get; set; }
        }
        public class PlayInfo : BaseInfo
        {
            public DateTime? PlayDate { get; set; }
            public TimeSpan? Duration { get; set; }
        }
        public class RatingInfo : PlayInfo
        {
            public int? RatingValue { get; set; }
        }
        public class FlagInfo : PlayInfo
        {
            public string FlagName { get; set; }
        }

        public static IEnumerable<PlayInfo> Plays()
        {
            return Db.Connection().Select<PlayInfo>
                (
                    @"
                        select datetime(play_date) as the_date,t.folder_name,t.file_name,p.play_duration
                        from plays p
                        join tracks t on p.track_id=t.track_id
                    ",
                    (reader) => new PlayInfo()
                    {
                        PlayDate = reader.AsDate("the_date"),
                        FolderName = reader.AsString("folder_name"),
                        FileName = reader.AsString("file_name"),
                        Duration = reader.AsTimeSpan("play_duration")
                    }
                 );
        }

        public static IEnumerable<RatingInfo> Ratings()
        {
            return Db.Connection().Select<RatingInfo>
                (
                    @"
                        select datetime(rating_date) as the_date,rating_value,t.folder_name,t.file_name
                        from ratings p
                        join tracks t on p.track_id=t.track_id
                    ",
                    (reader) => new RatingInfo()
                    {
                        PlayDate = reader.AsDate("the_date"),
                        RatingValue = reader.AsInteger("rating_value"),
                        FolderName = reader.AsString("folder_name"),
                        FileName = reader.AsString("file_name")
                    }
                 );
        }

        public static IEnumerable<FlagInfo> Flags()
        {
            return Db.Connection().Select<FlagInfo>
                (
                    @"
                        select t.folder_name,t.file_name,f.track_flag_name
                        from link_track_flag k
                        join track_flags f on k.track_flag_id=f.track_flag_id
                        join tracks t on k.track_id=t.track_id
                    ",
                    (reader) => new FlagInfo()
                    {
                        FolderName = reader.AsString("folder_name"),
                        FileName = reader.AsString("file_name"),
                        FlagName = reader.AsString("track_flag_name")
                    }
                );
        }
    }
}
